package com.zhanghe.study.design_pattern.singleton_pattern.lazy;

/**
 * 双重检查锁定  (可能导致获取到未初始化完全的对象)
 * @Author: zhanghe
 * @Date: 2020/4/21 18:02
 */
public class TwiceCheckSingleton {
    private static TwiceCheckSingleton instance = null;

    private TwiceCheckSingleton(){

    }

    public static TwiceCheckSingleton newInstance(){
        // 在需要的时候进行创建，如果已存在，直接返回之前的对象
        if(instance == null){
            synchronized(TwiceCheckSingleton.class){
                if(instance == null){
                    instance = new TwiceCheckSingleton();
                    System.out.println("创建单例实例1");
                }
                System.out.println("创建单例实例2");
            }
        }
        return instance;
    }
}
